package com.core.gui.helper.model;


import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.core.entities.Contact;
import com.core.gui.helper.ContactHelper;

public class ContactTableModel extends AbstractTableModel {
	
	private String[] columnNames;
	private Object[][] data;
	private List<Contact> contacts;
	
	public Contact getContact(int index){
		return contacts.get(index);
	}
	
	public ContactTableModel(List<Contact> contacts){
		columnNames = ContactHelper.getHeaders();
		updateModel(contacts);
	}
	public void updateModel(List<Contact> contacts){
		setContacts(contacts);
		data = ContactHelper.getContacts(contacts);
	}


	public int getColumnCount() {

		return columnNames.length;
	}


	public int getRowCount() {

		return data.length;
	}


	public Object getValueAt(int row, int col) {

		   try{
	            return data[row][col];
	        }catch(Exception e){
	            return "0";
	        }

	}
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
	
	public String getColumnName(int column){
		return columnNames[column];
	}
	private List<Contact> getContacts() {
		return contacts;
	}
	private void setContacts(List<Contact> contacts) {
		this.contacts = contacts;
	}
    public boolean isCellEditable(int row, int col) {
        return false;
    }
	

}
